//
// Created by xumy on 2021/7/27.
//    多态示例
//

# include "iostream"

using namespace std;

class Shape {
protected:
    int width, height;
public:
    Shape(int a = 0, int b = 0) {
        width = a;
        height = b;
    }

//    virtual int area();       // 错误，虚函数不能只声明，不定义，

    virtual int area() {};           // 正确，函数体为空

    // 提供接口框架的纯虚函数。若使用纯函数，则Shape成为一个抽象类(ABC)，则在实例化Shape的子类之前，必须实现每个虚函数，否则编译错误。
    // virtual int area() = 0;         // 正确，纯虚函数，没有函数体

};

class Rectangle : public Shape {
public:
    Rectangle(int a = 0, int b = 0) : Shape(a, b) {}

    int area() {
        cout << "Rectangle class area :";
        return (width * height);
    }
};

class Triangle : public Shape {
public:
    Triangle(int a = 0, int b = 0) : Shape(a, b) {}

    int area() {
        cout << "Triangle class area :";
        return (width * height / 2);
    }
};

int main() {

    Shape *shape;
    Rectangle rec(10, 7);
    Triangle tri(10, 5);


    // 存储矩形的地址
    shape = &rec;
    // 调用矩形的求面积函数 area
    cout << shape->area() << endl;

    // 存储三角形的地址
    shape = &tri;
    // 调用三角形的求面积函数 area
    cout << shape->area() << endl;

    return 0;
}